//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package docker_dao

import (
	"context"
	"fmt"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"atomgit.com/openlinksaas/proto-gen-go.git/docker_template_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type TemplateInfoDbItem struct {
	TemplateId string `bson:"_id" json:"_id"`
	AppId      string `bson:"appId" json:"appId"`
	Version    string `bson:"version" json:"version"`
	FileId     string `bson:"fileId" json:"fileId"`
	TimeStamp  int64  `bson:"timeStamp" json:"timeStamp"`
}

func (item *TemplateInfoDbItem) ToTemplateInfo() *docker_template_api.TemplateInfo {
	return &docker_template_api.TemplateInfo{
		AppId:   item.AppId,
		Version: item.Version,
		FileId:  item.FileId,
	}
}

type _TemplateDao struct {
	mc *mongo.Collection
}

func newTemplateDao(db *mongo.Database) (*_TemplateDao, error) {
	mc := db.Collection(db_ns.DB_NS_DOCKER + "template")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"appId", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_TemplateDao{mc: mc}, nil
}

func (db *_TemplateDao) Insert(ctx context.Context, dbItem *TemplateInfoDbItem) error {
	dbItem.TemplateId = db.genTemplateId(dbItem.AppId, dbItem.Version)
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_TemplateDao) genTemplateId(appId, version string) string {
	return fmt.Sprintf("%s:%s", appId, version)
}

func (db *_TemplateDao) Remove(ctx context.Context, appId, version string) error {
	templateId := db.genTemplateId(appId, version)
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": templateId})
	return err
}

func (db *_TemplateDao) Exist(ctx context.Context, appId, version string) (bool, error) {
	templateId := db.genTemplateId(appId, version)
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": templateId})
	return count > 0, err
}

func (db *_TemplateDao) Count(ctx context.Context, appId string) (uint32, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"appId": appId})
	return uint32(count), err
}

func (db *_TemplateDao) List(ctx context.Context, appId string) ([]*TemplateInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"appId": appId}, options.Find().SetSort(bson.M{"timeStamp": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*TemplateInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_TemplateDao) ListAll(ctx context.Context) ([]*TemplateInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{}, options.Find().SetSort(bson.M{"timeStamp": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*TemplateInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_TemplateDao) ListById(ctx context.Context, appIdList []string) ([]*TemplateInfoDbItem, error) {
	if len(appIdList) == 0 {
		return []*TemplateInfoDbItem{}, nil
	}
	cursor, err := db.mc.Find(ctx, bson.M{"appId": bson.M{"$in": appIdList}}, options.Find().SetSort(bson.M{"timeStamp": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*TemplateInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}
